package com.hckjsec.web.controller.tool;


import com.alibaba.fastjson.JSON;
import com.hckjsec.cloud.controller.AppletsConfigController;
import com.hckjsec.cloud.domain.AppletsConfig;
import com.hckjsec.cloud.domain.view.BasicView;
import com.hckjsec.cloud.service.IAppletsConfigParamDetailService;
import com.hckjsec.cloud.service.IAppletsConfigParamService;
import com.hckjsec.cloud.service.IAppletsConfigService;
import com.hckjsec.common.core.domain.entity.SysDept;
import com.hckjsec.common.core.domain.entity.SysUser;
import com.hckjsec.common.init.YwInitData;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.system.mapper.SysDeptMapper;
import com.hckjsec.system.service.ISysConfigService;
import com.hckjsec.system.service.ISysUserService;
import com.hckjsec.web.controller.system.SysUserController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Component
@Order(2)
public class ApplicationRunner  implements CommandLineRunner {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private YwInitData ywInitData;
    @Autowired
    private IAppletsConfigService appletsConfigService;
    @Autowired
    private IAppletsConfigParamDetailService appletsConfigParamDetailService;
    @Autowired
    private IAppletsConfigParamService appletsConfigParamService;
    @Autowired
    private ISysConfigService configService;

    @Override
    public void run(String... args) throws Exception {
        ConcurrentHashMap<String, String> BusinessStateMap= new ConcurrentHashMap<String, String>();
        BusinessStateMap.put("0","补材料");
        BusinessStateMap.put("1","待审核");
        BusinessStateMap.put("2","已审核");
        BusinessStateMap.put("98","已拒绝");
        BusinessStateMap.put("99","已撤销");
        ywInitData.initBusinessState(BusinessStateMap);
        userInit();
        deptInit();
        appidByIdInit();
//        System.out.println("初始化===============");
    }

    public void  userInit(){
        Map<Long, String> userMap = new Hashtable<Long, String>();
        List<SysUser> userList = userService.initUserList();
        userList.forEach(user -> {
            userMap.put(user.getUserId(),user.getNickName());
        });
        ywInitData.initUser(userMap);
        String domainName = configService.selectConfigByKey("sys.visit.domainName");
        ywInitData.initAccessUrlPrefix(domainName);
    }
    public void  deptInit(){
        Map<Long, String> deptMap = new Hashtable<Long, String>();
        List<SysDept> deptList = deptMapper.initDeptList();
        deptList.forEach(dept -> {
            deptMap.put(dept.getDeptId(),dept.getDeptName());
        });
        ywInitData.initDept(deptMap);
    }

    public void  appidByIdInit(){
        ConcurrentHashMap<String, Long> getAppidByIdMap= new ConcurrentHashMap<String, Long>();
        ConcurrentHashMap<Long, Object> IdByBasicViewMap= new ConcurrentHashMap<Long, Object>();
        ConcurrentHashMap<Long, Object> IdByConfigViewMap= new ConcurrentHashMap<Long, Object>();
        ConcurrentHashMap<Long, Object> IdByConfigStateMap= new ConcurrentHashMap<Long, Object>();
        List<AppletsConfig> list = appletsConfigService.selectAppletsConfigList(null);
        list.forEach(kv -> {
            getAppidByIdMap.put(kv.getAppid(),kv.getId());
            BasicView basicView=appletsConfigParamDetailService.getBasicView(kv.getId());
            IdByBasicViewMap.put(kv.getId(),basicView);
            AppletsConfig appletsConfig=appletsConfigService.selectAppletsConfigById(kv.getId());
            IdByConfigViewMap.put(kv.getId(),appletsConfig);
            List<Map<String,String>> stateMap= appletsConfigParamService.findMap(kv.getId());
            IdByConfigStateMap.put(kv.getId(),ComUtil.mapListByMap(stateMap,"paramnumber"));
        });
        ywInitData.initAppidById(getAppidByIdMap);
        ywInitData.initIdByBasicView(IdByBasicViewMap);
        ywInitData.initIdByConfigView(IdByConfigViewMap);
        ywInitData.initIdByConfigState(IdByConfigStateMap);
    }
}
